layui.use(['table', 'admin', 'ax', 'func', 'laydate', 'form','layer'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var laydate = layui.laydate;
    var form = layui.form;
    var layer = layui.layer;

    /**
     * 门店营业额报备表管理
     */
    var BtyMdYydz = {
        tableId: "btyMdYydzTable"
    };

    //渲染时间选择框
    let monthNum = new Date().getMonth() + 1;
    laydate.render({
        elem: '#date',
        format:'yyyy-MM',
        value:new Date().getFullYear() + "-" + monthNum,//默认当前月
        type: 'month',
        done:function(value,date,startDate){

        }
    });

    /**
     * 检索城市
     */
    var languageAjax = new $ax(Feng.ctxPath + "/btyMdYydz/cityList");
    var languageResult = languageAjax.start();
    $.each(languageResult.data, function (index, val) {
        $("#city").append("<option value='" + val.city + "'>" + val.city + "</option>");
    });
    form.render('select');

    /**
     * 初始化表格的列
     */
    BtyMdYydz.initColumn = function () {
        return [[
            // {type: 'checkbox'},
            {type: 'numbers',align:'center', title: '序号'},
            {field: 'id', hide: true, title: '主键id'},
            {field: 'dm', title: '店名', width: 220},
            {field: 'yaoma', title: '妖码',width:90},
            {field: 'cwm', title: '财务码',width:80},
            {field: 'city', title: '城市区域',width:120},
            {field: 'yyjl', title: '营运经理',width:100},
            {field: 'dd', title: '督导',width:80},
            {field: 'ddz', title: '大店长',width:80},
            {field: 'dz', title: '店长',width:80},
            {field: 'dzTel', title: '店长电话',width:120},
            {field: 'proType', title: '问题状态',width:90, templet: function (d){
                if (d.proType == 1){
                    return "需核对";
                }else if (d.proType == 2){
                    return "已解决";
                }else if (d.proType == 3){
                    return "无";
                }
                }},
            {field: 'bbCon', title: '报备数', width: 80},
            {field: 'cwCon', title: '财务审核情况', width: 115},
            // {field: 'yskj', title: '应收会计'},
            // {field: 'yyTel', sort: true, title: '营运经理电话'},
            // {field: 'ddTel', sort: true, title: '督导电话'},
            // {field: 'ddzTel', sort: true, title: '大店长电话'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    BtyMdYydz.search = function () {
        var queryData = {};
        queryData['city'] = $("#city").val();
        queryData['dm'] = $("#dm").val();
        queryData['proType'] = $("#proType").val();
        queryData['date'] = $("#date").val();

        table.reload(BtyMdYydz.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    BtyMdYydz.openAddDlg = function () {
        func.open({
            title: '添加门店营业额报备表',
            content: Feng.ctxPath + '/btyMdYydz/add',
            tableId: BtyMdYydz.tableId
        });
    };

    /**
     * 同步店名
     */
    BtyMdYydz.btnSyn = function () {

        var ajax = new $ax(Feng.ctxPath + "/btyMdYydz/synList", function (data) {
            Feng.success("同步成功!");
            table.reload(BtyMdYydz.tableId);
         }, function (data) {
            Feng.error("同步失败!" + data.responseJSON.message + "!");
        });
        ajax.start();

    };

     /**
      * 点击编辑
      *
      * @param data 点击按钮时候的行数据
      */
      BtyMdYydz.openEditDlg = function (data) {
          func.open({
              title: '修改门店营业额报备表',
              content: Feng.ctxPath + '/btyMdYydz/edit?id=' + data.id,
              tableId: BtyMdYydz.tableId
          });
      };


    /**
     * 导出excel按钮
     */
    // BtyMdYydz.exportExcel = function () {
    //     var checkRows = table.checkStatus(BtyMdYydz.tableId);
    //     if (checkRows.data.length === 0) {
    //         Feng.error("请选择要导出的数据");
    //     } else {
    //         table.exportFile(tableResult.config.id, checkRows.data, 'xls');
    //     }
    // };

    /**
     * 导出excel按钮
     */
    BtyMdYydz.exportExcel = function (data) {
        var date = $("#date").val();
        //获取导出Excel的数据
        var url = Feng.ctxPath + "/btyMdYydz/exportList?date=" + date;
        location.href = url;
    };

    /**
     * 跳转详情页
     * @param data
     */
    // BtyMdYydz.onDetailItem = function(data){
    //     var date = $("#date").val();
    //     window.location.href = Feng.ctxPath + '/btyMdYydzDetail/?yydzId=' + data.id + '&date=' + date + '&code=' + data.code;
    // };

    /**
     * 详情页弹窗
     * @param data
     */
    BtyMdYydz.onDetailItem = function(data){
        var date = $("#date").val();
        var index=layer.open({
            type: 2,
            title: false,
            content: Feng.ctxPath + '/btyMdYydzDetail/?yydzId=' + data.id + '&date=' + date + '&code=' + data.code,
            tableId: BtyMdYydz.tableId
        });
        layer.full(index)
        // window.location.href = Feng.ctxPath + '/btyMdYydzDetail/?yydzId=' + data.id + '&date=' + date + '&code=' + data.code;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    BtyMdYydz.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/btyMdYydz/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(BtyMdYydz.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var mdTol;
    var dzTol;
    var cwTol;
    var tableResult = table.render({
        elem: '#' + BtyMdYydz.tableId,
        url: Feng.ctxPath + '/btyMdYydz/list',
        page: true,
        height: "full-94",
        cellMinWidth: 100,
        limit: 20,
        cols: BtyMdYydz.initColumn(),
        done:function (res) {
            // list = res.data
            if (res.data == ''){
                $('#dzTol').text(0);
                $('#mdTol').text(0);
                $('#cwTol').text(0);
            } else {
                mdTol = res.data[0].mdTol;
                dzTol = res.data[0].dzTol;
                cwTol = res.data[0].cwTol;
                $('#dzTol').text(dzTol);
                $('#mdTol').text(mdTol);
                $('#cwTol').text(cwTol)
            }
        }
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        BtyMdYydz.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

    BtyMdYydz.openAddDlg();

    });

    // 导出excel
    $('#btnExp').click(function () {
        BtyMdYydz.exportExcel();
    });

    // 同步店名按钮
    $('#btnSyn').click(function () {
        BtyMdYydz.btnSyn();
    });

    // 工具条点击事件
    table.on('tool(' + BtyMdYydz.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            BtyMdYydz.openEditDlg(data);
        } else if (layEvent === 'delete') {
            BtyMdYydz.onDeleteItem(data);
        } else if (layEvent === 'detail'){
            BtyMdYydz.onDetailItem(data);
        }
    });
});
